<template>
  <div class="params">
    <BreadCrumb :item="['商品管理', '分类参数']"></BreadCrumb>
    <el-card class="box-card">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数"
        type="warning"
        show-icon=""
      >
      </el-alert>
      <div>
        <span>选择商品分类：</span>
        <el-cascader
          v-model="value"
          :options="goodsCategories"
          :props="treeProps"
          @change="handleChange"
        >
        </el-cascader>
      </div>
      <!-- 动态参数 静态属性 -->
      <el-tabs v-model="name" @tab-click="changeParams">
        <el-tab-pane label="动态参数" name="first" lazy>
          <el-button
            type="primary"
            @click="handleParams('add')"
            :disabled="!value.length"
            >添加动态属性</el-button
          >
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="second" lazy>
          <el-button
            type="primary"
            @click="handleParams('add')"
            :disabled="!value.length"
            >添加静态属性</el-button
          >
        </el-tab-pane>
      </el-tabs>
      <el-table :data="dataMany" border style="width: 100%">
        <el-table-column prop="attr_name" label="参数名称"> </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handleParams('edit', row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeParams(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹层 -->
    <el-dialog
      :title="selTitle"
      :visible.sync="isHandleParams"
      @close="handleClose"
    >
      <el-form ref="selForm" :model="form" :rules="rules">
        <el-form-item label="活动名称" prop="paramsName">
          <el-input v-model="form.paramsName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="subParams">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addGoodsSel,
  delGoodsParams,
  editGoodsSel,
  getGoodsAttribute,
  getGoodsParams,
} from '@/api/goods'
import { mapState } from 'vuex'
import { Message } from 'element-ui'
export default {
  name: 'aaa',
  data() {
    return {
      row_id: '',
      sel: 'many',
      type: '',
      params: '动态参数',
      isHandleParams: false,
      value: [],
      name: 'first',
      dataMany: [],
      treeProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
      },
      form: {
        paramsName: '',
      },
      rules: {
        paramsName: [
          { required: true, message: '输入不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  computed: {
    ...mapState('goods', ['goodsCategories']),
    goodsId() {
      return this.value[this.value.length - 1]
    },
    selTitle() {
      if (this.type === 'add') return '添加' + this.params
      if (this.type === 'edit') return '编辑' + this.params
      return ''
    },
  },
  methods: {
    // 获取参数
    async getParams(params) {
      const res = await getGoodsAttribute(this.goodsId, {
        sel: params,
      })
      this.dataMany = res.data
    },
    // 次联选择器
    handleChange(value) {
      this.value = value
      if (this.name === 'first') {
        this.getParams('many')
      }
      if (this.name === 'second') {
        this.getParams('only')
      }
    },
    // 切换参数
    changeParams(tab) {
      if (!this.value.length) {
        Message.error('请输入参数')
      }
      if (tab.name === 'first') {
        this.params = '动态参数'
        this.sel = 'many'
        this.getParams('many')
      }
      if (this.name === 'second') {
        this.params = '静态参数'
        this.sel = 'only'
        this.getParams('only')
      }
    },
    // 操作参数
    async handleParams(type, row) {
      if (!this.value.length) return Message.error('请输入参数')
      this.type = type
      if (this.type === 'edit') {
        this.row_id = row.attr_id
        const res = await getGoodsParams(this.goodsId, row.attr_id, {
          sel: this.sel,
        })
        // console.log(res)
        this.form.paramsName = res.data.attr_name
      }

      this.isHandleParams = true
    },
    // 提交参数
    async subParams() {
      if (this.type === 'add') {
        await addGoodsSel(this.goodsId, {
          attr_name: this.form.paramsName,
          attr_sel: this.sel,
        })
      }
      if (this.type === 'edit') {
        await editGoodsSel(this.goodsId, this.row_id, {
          attr_name: this.form.paramsName,
          attr_sel: this.sel,
        })
      }
      this.getParams(this.sel)
      this.isHandleParams = false
    },
    // 删除参数
    async removeParams(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await delGoodsParams(this.goodsId, row.attr_id)
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.getParams(row.attr_sel)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    handleClose() {
      this.form = {
        paramsName: '',
      }
      this.$refs.selForm.resetFields()
    },
  },
  created() {
    this.$store.dispatch('goods/initCategories')
  },
}
</script>

<style lang="less" scoped>
.el-alert,
.el-button {
  margin-bottom: 15px;
}
</style>
